package com.softer.chengxin.dicinfo.dao.provider;

import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.dicinfo.entity.DicType;
import org.apache.commons.lang3.StringUtils;

/**
 * @author zcz
 * @date 2018/12/20
 */
public class DicTypeDaoProvider {
    /**
     * 字典类型列表
     */
    public String getList(Page page){
        StringBuffer sql = new StringBuffer(10);
        sql.append("SELECT d.`id`,d.`code`,d.`name`,d.`adder`,DATE_FORMAT(d.add_time, '%Y-%m-%d') AS `addTime`,d.`description` FROM dic_type d  WHERE `code` != '1010'");
        sql.append(" LIMIT ");
        sql.append(page.getBeginIndex()).append(",").append(page.getLimit());
        return sql.toString();
    }
    /**
     * 修改字典类型信息
     * @param dicType
     * @return
     */
    public String update(DicType dicType) {
        StringBuffer sql = new StringBuffer(20);
        sql.append("UPDATE dic_type SET add_time = NOW() ");
        commonSql(sql, dicType);
        System.out.println("修改sql:"+sql.toString());
        return sql.toString();
    }

    private void commonSql(StringBuffer sql, DicType dicType) {
        if(StringUtils.isNotBlank(dicType.getAdder())){
            sql.append(" ,adder=#{adder} ");
        }
        if(StringUtils.isNotBlank(dicType.getDescription())){
            sql.append(" ,description=#{description} ");
        }
        if(StringUtils.isNotBlank(dicType.getName())){
            sql.append(" ,name=#{name} ");
        }
        sql.append(" where id=#{id}");
    }
}
